package com.qk.controller;

import com.qk.aop.anno.LogRecord;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.user.UserDO;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: zhangchuang
 * @Date: 2025/10/07 14:37
 * @Description: 用户接口
 */
@RestController
@RequestMapping("/users")
@Slf4j
@SuppressWarnings("all")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 根据部门查询用户
     *
     * @param deptId
     * @return Result
     */
    @GetMapping("/dept/{deptId}")
    public Result getByDept(@PathVariable(name = "deptId") Integer deptId) {
        log.info("根据部门查询用户，参数：{}", deptId);
        List<UserDO> list = userService.getByDept(deptId);
        return Result.success(list);
    }

    /**
     * 根据角色查询用户
     *
     * @param label
     * @return Result
     */
    @GetMapping("/role/{roleLabel}")
    public Result getByRole(@PathVariable(name = "roleLabel") String label) {
        log.info("根据角色查询用户，参数：{}", label);
        List<UserDO> list = userService.getByRole(label);
        return Result.success(list);
    }

    /**
     * 修改用户
     *
     * @param user
     * @return Result
     */
    @LogRecord
    @PutMapping
    public Result updateUser(@RequestBody User user) {
        log.info("修改用户，参数：{}", user);
        userService.updateById(user);
        return Result.success();
    }

    /**
     * 根据id查询用户
     *
     * @param id
     * @return Result
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询用户，参数：{}", id);
        UserDO user = userService.getById(id);
        return Result.success(user);
    }

    /**
     * 删除用户
     *
     * @param ids
     * @return Result
     */
    @LogRecord
    @DeleteMapping("/{ids}")
    public Result deleteUser(@PathVariable String ids) {
        log.info("删除用户，参数：{}", ids);
        userService.deleteUsers(ids);
        return Result.success();
    }

    /**
     * 新增用户
     *
     * @param user
     * @return Result
     */
    @LogRecord
    @PostMapping
    public Result addUser(@RequestBody User user) {
        log.info("新增用户，参数：{}", user);
        userService.addUser(user);
        return Result.success();
    }

    /**
     * 分页查询所有用户
     *
     * @param userDto
     * @return Result
     */
    @GetMapping
    public Result getByPage(UserDTO userDTO) {
        log.info("分页查询所有用户，参数：{}", userDTO);
        PageResult<UserDO> userPageResult = userService.listPage(userDTO);
        return Result.success(userPageResult);
    }

}
